<template>
	<view class="page_wrapper page_bg">

	</view>
</template>

<script>
	import {request} from '@/utils/request.js'
	import {utils} from '@/utils/utils.js'
	import {storageData} from '@/utils/storageData.js'
	export default {
		data() {
			return {
				tipFlag: true,
				userInput: '',
				firstNum: 0, // 第一个数是6到9之间的数。
				secondNum: 0, // 第二个数是比第一个数小，但加起来是大于10的数。
				inputFirstNum: 0,
				inputSecondNum: 0,
				answerNum: 0,
				userInputFirstNum: 0,
				userInputSecondNum: 0,
				userInputAnswerNum: 0,
				currentInput: 0,
				playMusicStatus: false,
				kouJueMusicUrl: '',
				audioContext: {},
				currentPlayMusic: '',
				mathExam: {
				},
				calcList: [],
				currentCalcIndex: -1
			}
		},
		onLoad() {
			const apiUrl = 'https://api.aliyun.com/bailian';
			const params = {
			  key: 'your_api_key',
			  text: 'your_input_text',
			};
			
			
			uni.request({
				url:apiUrl,
				data: params,
			  method: 'POST',
			  responseType: 'blob', // 设置为 'blob' 接收流数据
				header: {
					'Content-Type'	: 'application/x-www-form-urlencoded',
					'Authorization'	: accessToken
				},
				onDownloadProgress(progressEvent) {
				  // 处理流数据
				  console.log(`已下载数据大小：${progressEvent.loaded}`);
				  console.log(`总数据大小：${progressEvent.total}`);
				  const percentComplete = progressEvent.loaded / progressEvent.total * 100;
				  console.log(`下载进度：${percentComplete.toFixed(2)}%`);
				  
				  // 在这里你可以将数据分块处理，例如上传到服务器或在本地处理
				  // 注意，这里只是示例，实际处理可能需要根据具体需求进行
				  const chunkSize = 1024 * 1024; // 分块大小，例如1MB
				  const start = progressEvent.loaded - chunkSize > 0 ? progressEvent.loaded - chunkSize : 0;
				  const end = progressEvent.loaded;
				  const blobSlice = progressEvent.data.slice(start, end);
				  // 对 blobSlice 进行进一步处理，例如上传到服务器
				},
				success: (response) => {
					console.log(response)
					console.log('请求成功')
				},
				fail: (error) => {
					console.log('请求失败')
				},
			});
			
			uniRequest({
			  url: apiUrl,
			  data: params,
			  
			}).then((res) => {
			  // 请求成功后的处理
			  console.log('请求成功:', res);
			}).catch((err) => {
			  // 请求失败的处理
			  console.error('请求错误:', err);
			})
		},
		methods: {
			tryAgain() {
				this.fetchCalcData()
			},
			fetchCalcData() {
				this.currentCalcIndex = -1
				uni.showLoading()
				// request.postJson( 
				// 	'/mathExams/pingShiFa', {}
				// ).then(res => {
				// 	uni.hideLoading()
				// 	console.log(res)
				// 	this.mathExam = res
				// 	this.calcList = res.calcLogList
				// 	this.nextCalc()
				// }).catch(err => {
				// 	uni.hideLoading()
				// 	console.log(err)
				// })
			},
			fetchMusicData(){
				// 口诀
				// request.get(
				// 	'/musicSources/idNo,1716813912931', {}
				// ).then(res => {
				// 	this.kouJueMusicUrl = res.musicUrl
				// }).catch(err => {
				// 	console.log(err)
				// })
				
			},
			
			nextCalc() {
				this.currentCalcIndex++
				
				if(this.currentCalcIndex >= this.calcList.length){
					const $this = this
					
					uni.showModal({
						title: '提示',
						content: '您已练完一组，是否再练一组?',
						cancelText: '我累了',
						confirmText: '我还能再练',
						success: function (res) {
							if (res.confirm) {
								$this.fetchCalcData()
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
					return false
				}
				
				const calc = this.calcList[this.currentCalcIndex]
				this.firstNum = calc.firstNum
				this.secondNum = calc.secondNum
				this.answerNum = calc.resultNum
				this.inputFirstNum = this.firstNum - 10
				this.inputSecondNum = this.secondNum - this.inputFirstNum
				
				
				this.userInputFirstNum = 0
				this.userInputSecondNum = 0
				this.userInputAnswerNum = 0
				this.currentInput = 0
				
			},
			getRandomNumber(maxNum) {  
			    return Math.floor(Math.random() * maxNum) + 1;  
			},
			getSecondRandomNumber(maxNum) {  
			    return Math.floor(Math.random() * maxNum);  
			},
			getRandomBoolean() {  
			    return Math.random() >= 0.5;  
			},
			showTip() {
				this.tipFlag = !this.tipFlag
			},
			playKouJueMusic(musicUrl) {
				if(this.playMusicStatus === true){
					// this.audioContext.stop()
					this.playMusicStatus = false
					
					if(this.currentPlayMusic === musicUrl){
						// this.audioContext.destroy()
						// this.audioContext = null
						return false
					}
					
				}
				this.currentPlayMusic = musicUrl
				this.playMusicStatus = true
				console.log('play music:' + musicUrl)
				const $this = this
				const audioContext = uni.createInnerAudioContext()
				audioContext.autoplay = true
				audioContext.src = musicUrl
				audioContext.onEnded(() => {
					console.log('music end')
					$this.playMusicStatus = false
					audioContext.destroy()
				})
				
			},
			keyDown(num) {
				// 比较结果
				console.log(num)
				let result = ''
				if(this.currentInput === 0) {
					if(this.inputFirstNum === num) {
						result = '正确'
						this.userInputFirstNum = num
						this.currentInput++
					}else{
						result = '错误'
					}
				}else if(this.currentInput === 1) {
					if(this.inputSecondNum === num) {
						result = '正确'
						this.userInputSecondNum = num
						this.currentInput++
					}else{
						result = '错误'
					}
				}else if(this.currentInput === 2) {
					if(this.answerNum === num) {
						result = '正确'
						this.userInputAnswerNum = num
						this.currentInput++
					}else{
						result = '错误'
					}
				}
				if(result !== '') {
					uni.showToast({
						icon: 'none',
						title: result,
						duration: 1000
					})
					if(result === '错误') {
						return false
					}
				}
				const $this = this
				if(this.currentInput > 2){
					setTimeout(() => {
						$this.currentInput = 0
						$this.nextCalc()
					}, 500)
				}
				
			}
		},
		onShareAppMessage(res) {
			const userInfo = storageData.getLoginUser()
			if (res.from === 'button') {
					 // 来自页面内分享按钮
					 console.log(res.target);
			}
			return {
				title: '小奕口算：平十法练习',
				path: '/pages/qiMengIndex/qiMengIndex?shareUc=' + userInfo.userCode,
				mpId: 'wxcbbe1a868c5c3c3c', //此处配置微信小程序的AppId
			}
		},
		onShareTimeline(res) {
			const userInfo = storageData.getLoginUser()
			return {
				title: '小奕口算：平十法练习',
				path: '/pages/qiMengIndex/qiMengIndex?shareUc=' + userInfo.userCode,
				mpId: 'wxcbbe1a868c5c3c3c', //此处配置微信小程序的AppId
			}
		}
	}
</script>

<style lang="scss" scoped>
	

</style>
